﻿using AliveCMS.Models;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace AliveCMS.Controllers
{
    public class PageDataController : ApiController
    {
        // GET api/pagedata
        public IEnumerable<PageViewModel> Get()
        {
            return MvcApplication.Pages.ToList();
        }

        // GET api/pagedata/5
        public PageViewModel Get(Guid id)
        {
            return MvcApplication.Pages.FirstOrDefault(n => n.PageId == id);
        }

        // POST api/pagedata
        public void Post([FromBody]PageViewModel value)
        {
            Document doc = new Document();

            MvcApplication.Pages.Add(value);
            using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
            {
                doc.Add(new Field("ObjectName", "PageViewModel", Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("PageId", value.PageId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("SiteId", value.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Name", value.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("ParentPageId", (value.ParentPageId.HasValue ? value.ParentPageId.Value.ToString() : null), Field.Store.YES, Field.Index.NOT_ANALYZED));
                indexWriter.AddDocument(doc);
                indexWriter.Optimize();
            }
        }

        // PUT api/pagedata/5
        public void Put(Guid id, [FromBody]PageViewModel value)
        {
            Document doc = new Document();
            PageViewModel page = MvcApplication.Pages.FirstOrDefault(n => n.PageId == value.PageId);

            if (page != null)
            {
                using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
                {
                    doc.Add(new Field("ObjectName", "PageViewModel", Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("PageId", value.PageId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("SiteId", value.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("Name", value.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("ParentPageId", (value.ParentPageId.HasValue ? value.ParentPageId.Value.ToString() : null), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    indexWriter.UpdateDocument(new Term("PageId", value.PageId.ToString()), doc);
                    indexWriter.Optimize();
                }
            }
        }

        // DELETE api/pagedata/5
        public void Delete(Guid id)
        {
            MvcApplication.Pages.RemoveAll(n => n.PageId == id);
            using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
            {
                indexWriter.DeleteDocuments(new Term("PageId", id.ToString()));
            }
        }
    }
}
